package com.huixue.cn.ztej.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huixue.cn.ztej.bean.Project;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.enums.RoleStatus;
import com.huixue.cn.ztej.service.SysProjectService;
import com.huixue.cn.ztej.service.SysUserService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.annotation.RequestMappingName;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Controller
@RequestMapping(value = "/sys/sysProjectMan")
public class ProjectController extends BaseController {
	private static Logger logger = LoggerFactory
			.getLogger(ProjectController.class);
	@Autowired(required = true)
	SysUserService sysUserService;
	
	@Autowired(required = true)
	SysProjectService sysProjectService;

	/**
	 * <p>跳转到列表页面</p>
	 */
	@RequestMappingName(value = "跳转到列表页面")
	@RequestMapping(value = "ProjectInfo/toSearch",method = RequestMethod.GET)
	public String toSearch(@RequestParam(value = "proCode", defaultValue = "") String proCode,@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			HttpServletRequest request,HttpServletResponse response,Model model) {
		Map<String, Object> searchParams = new HashMap<String,Object>();
		searchParams.put("proCode",proCode.trim());
		User currentUser = (User)request.getSession().getAttribute("loginUser");

		//项目管理
		if(currentUser.getRoleId()!=null && currentUser.getRoleId().equals(RoleStatus.ROLE_8.getRoleId()+"")){
			searchParams.put("proId", currentUser.getProjectID());
		}
		
		searchParams.put("userName",currentUser);
		Results results = sysProjectService.searchDtosByPage(searchParams,page,rows);
		model.addAttribute("res", results);

		return "/com/ztej/project/sysProInfo";
	}
	
	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到添加页面")
	@RequestMapping(value = "ProjectInfo/toAdd", method = RequestMethod.GET)
	public String toAdd(Model model) {
		return "/com/ztej/project/sysProInfoAdd";
	}
	
	/**
	 * <p>增加项目基本信息</p>
	 */
	@RequestMappingName(value = "增加用户基本信息")
	@RequestMapping(value = "/ProjectInfo/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	@ResponseBody
	public Results add(@RequestBody Project sysProInfo,HttpServletRequest request) {

		Results results = null;	
		results = sysProjectService.add(sysProInfo);
		logger.info(new JacksonUtil().getJson(results));
	
		return results;
	}
	
	
	/**
	 * <p>跳转到添加页面</p>
	 */
	@RequestMappingName(value = "跳转到更新页面")
	@RequestMapping(value = "ProjectInfo/toModify/{proId}", method = RequestMethod.GET)
	public String toModify(@PathVariable("proId")Integer proId,Model model,HttpServletRequest request) {
		Results results = sysProjectService.findByProId(proId);
		model.addAttribute("sysProInfo", results.getData());
		return "/com/ztej/project/sysProInfoModify";
		
	}
	
	/**
	 * <p>增加项目基本信息</p>
	 */
	@RequestMappingName(value = "修改用户基本信息")
	@RequestMapping(value = "ProjectInfo/modify", method = RequestMethod.POST)
	@ResponseBody
	public Results modify(@RequestBody Project sysProInfo,HttpServletRequest request) {
		
		Results results = null;

		results = sysProjectService.modify(sysProInfo);
		
		logger.info(new JacksonUtil().getJson(results));
		
		return results;
	}
	
	/**
	 * <p>删除项目基本信息</p>
	 * 
	 */
	@RequestMappingName(value = "删除项目基本信息")
	@RequestMapping(value = "ProjectInfo/delete/{id}", method = RequestMethod.GET)
	public String delete(@PathVariable("id")Integer id,HttpServletRequest request) {
		
		Results results = null;
		try {
			
			results = sysProjectService.delete(id);
			
		} catch (Exception e) {
			results = new Results(
					API_RETURN_STATUS.SERVER_INTERNAL_ERROR.value(),
					API_RETURN_STATUS.SERVER_INTERNAL_ERROR.desc());
		}
		logger.info(new JacksonUtil().getJson(results));
		return "redirect:/sys/sysProjectMan/ProjectInfo/toSearch";
		
	}
	
		

}
